package com.mdinfo.gw.door.mapper

import com.baomidou.mybatisplus.mapper.BaseMapper
import com.mdinfo.gw.door.bean.Door
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

/**
 * Created by Administrator on 2018/1/11.
 */
interface DoorMapper : BaseMapper<Door> {

    /**根据门禁id查询绑定的门*/
    @Select("""<script>
        select * from door t where t.doorAccessId = #{doorAccessId}
    </script>""")
    fun listDoorByDoorAccess(@Param("doorAccessId")doorAccessId:Int): List<Door>


    /**列表条件查询*/
    @Select("""<script>
        select * from door t where 1=1
        <if test="key!=null">
            and (
                t.devId like CONCAT("%",#{key},"%")
                or
                t.name like CONCAT("%",#{key},"%")
                )
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
        order by t.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listQuery(@Param("start")start:Int,
                  @Param("num")num:Int,
                  @Param("key")key:String?,
                  @Param("deptId")deptId:Int?,
                  @Param("deptIdList") deptIdList: List<Int>)
            :List<Door>

    /**列表条件查询总数*/
    @Select("""<script>
        select count(*) from door t where 1=1
        <if test="key!=null">
            and (
                t.devId like CONCAT("%",#{key},"%")
                or
                t.name like CONCAT("%",#{key},"%")
                )
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
    </script>""")
    fun countListQuery(@Param("key")key:String?,
                       @Param("deptId")deptId:Int?,
                       @Param("deptIdList") deptIdList: List<Int>)
            :Int


    /**根据门设备号查询门*/
    @Select("""<script>
        select * from door t where t.devId = #{devId}
    </script>""")
    fun getBydevId(@Param("devId")devId:String):Door?


    /**获取本部门所有的门*/
    @Select("""<script>
        select * from door t where t.deptId = #{deptId}
    </script>""")
    fun listAllByDeptId(@Param("deptId")deptId:Int):List<Door>


    /**根据门卡id获取绑定的门*/
    @Select("""<script>
        select d.* from door_card_bind dcb
        join door d
        on (dcb.doorId = d.id and dcb.cardId = #{cardId})
    </script>""")
    fun listByCardId(@Param("cardId")cardId:Int)
        :List<Door>


    /**根据门禁id分页查询*/
    @Select("""<script>
        select * from door t
        where t.doorAccessId = #{accessId}
        order by t.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listByAccessId(@Param("accessId")accessId:Int,
                       @Param("start")start:Int,
                       @Param("num")num:Int)
            :List<Door>

    /**根据门禁id分页查询总数*/
    @Select("""<script>
        select count(*) from door t
        where t.doorAccessId = #{accessId}
    </script>""")
    fun countListByAccessId(@Param("accessId")accessId:Int)
            :Int

    /**根据部门id集合插叙*/
    @Select("""<script>
        select * from door t where 1=1
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
    </script>""")
    fun listByDeptIdList(@Param("deptIdList") deptIdList:List<Int>)
            :List<Door>

}